//
//  WCPopupLayerManager.m
//  WC
//
//  Created by 杨鑫 on 15/4/28.
//  Copyright (c) 2015年 智康. All rights reserved.
//

#import "WCPopupLayerManager.h"
#import "BTMarchs.h"
//#import "Constants.h"
//#import "WCNHudImageVIew.h"


@interface WCPopupLayerManager ()
{
    MBProgressHUD *_netwokerHud;
    

}
@property (nonatomic) BOOL keyBoardWillShow;

@end

@implementation WCPopupLayerManager

#pragma mark - Life

+ (instancetype)shareMg
{
    static WCPopupLayerManager *sharedMg = nil;
    //block
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedMg = [[WCPopupLayerManager alloc] initPrivate];
    });
    return sharedMg;
}

- (instancetype)init
{
    @throw [NSException exceptionWithName:@"Singleton"
                                   reason:@"Use + [WCPopupLayerManager sharedMg]"
                                 userInfo:nil];
    return nil;
}

- (instancetype)initPrivate
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - NSNotification

- (void)keyboardShow:(NSNotification *)note
{
    self.keyBoardWillShow = YES;
}

- (void)keyboardHide:(NSNotification *)note
{
    self.keyBoardWillShow = NO;
}

#pragma mark - Show HUB

- (MBProgressHUD *)showSimple:(UIView *)view
{
      
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];

    hud.removeFromSuperViewOnHide = YES;
//    hud.margin = 0;
    hud.delegate = self;
    
    //调整位置
    [self setYOffset:hud withView:view];
    
    [view addSubview:hud];
    
    [hud show:YES];
    
//    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
//    //调整位置
//    [self setYOffset:hud withView:view];
//    
//    hud.animationType = MBProgressHUDAnimationZoomIn;
//    hud.detailsLabelText = @"...";
//    [view addSubview:hud];
//    hud.mode = MBProgressHUDModeAnnularDeterminate;

    
    return hud;
}
- (void) showNetFaild
{
    _netwokerHud = [[MBProgressHUD alloc] initWithView:[UIApplication sharedApplication].keyWindow];
    _netwokerHud.delegate = self;
    _netwokerHud.labelText =@"您的网络不给力,请稍后再试~";
    _netwokerHud.animationType =MBProgressHUDAnimationZoom;
   
    _netwokerHud.mode =MBProgressHUDModeIndeterminate;
    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(disappearAction:)];
    [_netwokerHud addGestureRecognizer:tap];
    
    [[UIApplication sharedApplication].keyWindow addSubview:_netwokerHud];
    
    [_netwokerHud show:YES];
    



}
- (void)disappearAction:(UITapGestureRecognizer *)sender
{
    [_netwokerHud setHidden:YES];
 
}
- (MBProgressHUD *)showWithLabel:(NSString *)title withView:(UIView *)view
{
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
    hud.labelText = title;
    hud.delegate = self;
    
    //调整位置
    [self setYOffset:hud withView:view];
    
    [view addSubview:hud];
    
    [hud show:YES];
    
    return hud;
}

- (void)showTextOnly:(NSString *)txt withView:(UIView *)view
{
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
    hud.removeFromSuperViewOnHide = YES;
    hud.mode = MBProgressHUDModeText;
    hud.detailsLabelText = txt;
    hud.detailsLabelFont = [UIFont systemFontOfSize:15.0];
    
    //调整位置
    [self setYOffset:hud withView:view];
    
    [view addSubview:hud];
    [hud show:YES];
    [hud hide:YES afterDelay:1.2];
}

- (MBProgressHUD *)showSimpleInImageView:(UIView *)imageView
{
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:imageView];
    hud.delegate = self;
    hud.margin = 0;
//    hud.cornerRadius = 0;
    hud.color = [UIColor clearColor];
//    hud.activityIndicatorColor = [UIColor lightGrayColor];
    
    [imageView addSubview:hud];
    
    [hud show:YES];
    
    return hud;
}

- (MBProgressHUD *)showProgressToView:(UIView *)view{
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
    //调整位置
    [self setYOffset:hud withView:view];

    hud.animationType = MBProgressHUDAnimationZoomIn;
    hud.detailsLabelText = @"上传中...";
    [view addSubview:hud];
    hud.mode = MBProgressHUDModeAnnularDeterminate;
    
    [hud show:YES];

    return hud;
}
#pragma mark - Private

- (void)setYOffset:(MBProgressHUD *)hud withView:(UIView *)view
{
    UIView *topView = [UIApplication sharedApplication].keyWindow;
    CGRect rect = [view convertRect:view.bounds toView:view.superview];
    
    hud.yOffset = 0 - rect.origin.y/2 + (topView.frame.size.height - rect.origin.y - rect.size.height)/2;
    
    if (self.keyBoardWillShow) {
        hud.yOffset = hud.yOffset - kKeyboardHeight/2.0f;
    }
}

#pragma mark - MBProgressHUDDelegate

- (void)hudWasHidden:(MBProgressHUD *)hud
{
    [hud removeFromSuperview];
    hud = nil;
}




@end
